package com.lin.reggie.service.Impl;

import com.lin.reggie.entity.User;
import com.lin.reggie.mapper.UserMapper;
import com.lin.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    private UserMapper userMapper;
    /**
     * 作用：登录校验
     *
     * @param phone
     * @param code
     * @param codeInSession
     * @return
     */
    /**
     * @param phone 用户输入的手机号
     * @param code 用户输入的验证码
     * @param codeInSession 系统的验证码
     * @return
     * */
    @Override
    public User login(String phone, String code, String codeInSession) {
        //1.校验验证码是否为空并且两个验证码是否一致
        User dbUser=null;
        if (codeInSession!=null&&code.equalsIgnoreCase(codeInSession)){
            //验证码没有问题
            //2.根据手机号查询用户
            dbUser = userMapper.findByPhone(phone);
            if (dbUser==null){
                //3.如果空代表该号码是新用户，直接注册
                dbUser = new User();
                dbUser.setPhone(phone);
                dbUser.setStatus(1);
                userMapper.save(dbUser);
            }
        }
        return dbUser;
    }
}
